import axios from 'axios'

const request = axios.create({
  baseURL: import.meta.env.VITE_BASE_URL
})

request.interceptors.request.use(
  (config) => {
    const token = localStorage.getItem('token')
    if (token) {
      config.headers.Authorization = `Bearer ${token}`
    }

    return config
  },
  (err) => {
    return Promise.reject(err)
  }
)

// TODO:对响应进行拦截处理
request.interceptors.response.use(
  (response) => {
    return response.data.data || response.data
  },
  (err) => {
    return err.response?.data
  }
)

export default request
